using System.Collections.Generic;
using GameStateManagement.Model;
using GameStateManagement.Model.Entities;
using GameStateManagement.Model.Physics;
using GameStateManagement.Model.Physics.Interfaces;
using GameStateManagement.View.Screens;
using GameStateManagement.View.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameStateManagement.View.Rendering
{
    public class RenderManager
    {
        #region Fields

        private readonly BasicRenderer _renderer;

        #endregion

        #region Properties

        public SpriteBatch SpriteBatch { get; set; }
        public SpriteManager SpriteManager;

        #endregion

        #region Constructors

        public RenderManager(SpriteBatch spriteBatch, SpriteManager spriteManager)
        {
            SpriteBatch = spriteBatch;
            SpriteManager = spriteManager;
            _renderer = new BasicRenderer(this);
        }

        #endregion

        #region Public Methods

        //public void RenderGameModel(GameModel model)
        //{
        //    var decorations = model.GetDecorations();
        //    foreach (var decoration in decorations)
        //    {
        //        if (decoration.TextureName == null)
        //            _renderer.Render(decoration, "debug_texture");
        //        _renderer.Render(decoration);
        //    }

        //    var forceFields = model.Physics.GetForceFields();
        //    foreach (PhysicsForceField field in forceFields)
        //    {
        //        if (field.TextureName == null)
        //            _renderer.Render(field, "debug_texture");
        //        _renderer.Render(field);
        //    }

        //    var entities = model.Entities.GetArray();
        //    foreach (EntityBase ent in entities)
        //    {
        //        if (ent.TextureName == null)
        //            _renderer.Render(ent, "debug_texture");
        //        _renderer.Render(ent);
        //    }
        //}

        public void RenderDecorations(List<IRenderable> decoratons)
        {
            
        }

        public void RenderForceFields(List<PhysicsForceField> forceFields)
        {
            
        }

        public void RenderEntities(List<EntityBase> entities)
        {
            
        }

        public void LoadContent(GameplayScreen aScreen, List<string> textureNameList)
        {
            foreach (var textureName in textureNameList)
            {
                SpriteManager.LoadSprite(textureName);
            }
        }

        #endregion

        #region Collision Helper Methods

        public CollisionData GetCollisionData(ICollidable collidable)
        {
            var collisionData = new CollisionData();
            Texture2D texture = SpriteManager.GetSprite(collidable.TextureName);
            collisionData.BoundingBox = GetBoundingBox(collidable, texture);
            collisionData.TextureWidth = texture.Width;
            collisionData.TextureHeight = texture.Height;
            collisionData.TextureCollisionData = GetTextureCollisionData(collidable);
            collisionData.TextureOrigin = Renderer.GetOrigin(texture);

            return collisionData;
        }

        private static Rectangle GetBoundingBox(ICollidable collidable, Texture2D texture)
        {
            return new Rectangle(
                (int)(collidable.Position.X - (texture.Width * collidable.Scale.X) / 2),
                (int)(collidable.Position.Y - (texture.Width * collidable.Scale.Y) / 2),
                (int)(texture.Width * collidable.Scale.X),
                (int)(texture.Height * collidable.Scale.Y));
        }

        private Color[] GetTextureCollisionData(ICollidable collidable)
        {
            return SpriteManager.GetCollisionData(collidable.TextureName);
        }

        #endregion

    }
}
